﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuotationService.HistoryQuotation
{
    internal class StockEntityConverter :
        IEntityConverter<Quotation.Stock.BasicInfo, StockBasicInfo>,
        IEntityConverter<Quotation.Stock.QuotationInfo, StockQuotationInfo>,
        IEntityConverter<Quotation.Stock.TickInfo, StockTickInfo>,
        IEntityConverter<Quotation.Stock.DailyData, StockDailyData>,
        IEntityConverter<Quotation.Stock.MinuteDataCollection, StockMinuteDataCollection>
    {


        public Quotation.Stock.BasicInfo Convert(StockBasicInfo basicInfo)
        {
            return new Quotation.Stock.BasicInfo()
            {
                AccountDate = Quotation.Date.FromYMMDD(basicInfo.AccountDate),
                AccuredInterest = basicInfo.AccuredInterest,
                AnnualProfit = basicInfo.AnnualProfit,
                BaseStock = basicInfo.BaseStock,
                BeginDate = Quotation.Date.FromYMMDD(basicInfo.BeginDate),
                BondBonusDate = Quotation.Date.FromYMMDD(basicInfo.BondBonusDate),
                BondConvertRate = basicInfo.BondConvertRate,
                ComponentStockFlag = basicInfo.ComponentStockFlag,
                ConvertQty = basicInfo.ConvertQty,
                CurrencyType = basicInfo.CurrencyType,
                Decimal = basicInfo.Decimal,
                DynamicVer = basicInfo.DynamicVer,
                EndDate = Quotation.Date.FromYMMDD(basicInfo.EndDate),
                EnglishName = basicInfo.EnglishName,
                FinancingLoanFlag = basicInfo.FinancingLoanFlag,
                FinancingLoanTradingFlag = basicInfo.FinancingLoanTradingFlag,
                GuarantyConvertRate = basicInfo.GuarantyConvertRate,
                HighBuyQtyLimit = basicInfo.HighBuyQtyLimit,
                HighSellQtyLimit = basicInfo.HighSellQtyLimit,
                IndustryType = basicInfo.IndustryType,
                ISINCode = basicInfo.ISINCode,
                LastAnnualProfit = basicInfo.LastAnnualProfit,
                LevelFlag = basicInfo.LevelFlag,
                LowBuyQtyLimit = basicInfo.LowBuyQtyLimit,
                LowSellQtyLimit = basicInfo.LowSellQtyLimit,
                MarketMakerFlag = basicInfo.MarketMakerFlag,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinOrderPrice = basicInfo.MinOrderPrice,
                Name = basicInfo.Name,
                OpenPrice = basicInfo.OpenPrice,
                OrderPriceUnit = basicInfo.OrderPriceUnit,
                OtherBizStatus = basicInfo.OtherBizStatus,
                ParValue = basicInfo.ParValue,
                PreClosePrice = basicInfo.PreClosePrice,
                PreExchTotalKnockAmt = basicInfo.PreExchTotalKnockAmt,
                PreExchTotalKnockQty = basicInfo.PreExchTotalKnockQty,
                QuotationTime = basicInfo.QuotationTime,
                RepurchaseDay = basicInfo.RepurchaseDay,
                SecuritiesLoanFlag = basicInfo.SecuritiesLoanFlag,
                SecuritiesLoanSellPriceLimitFlag = basicInfo.SecuritiesLoanSellPriceLimitFlag,
                SecuritiesLoanTradingFlag = basicInfo.SecuritiesLoanTradingFlag,
                ShortPrefix = basicInfo.ShortPrefix,
                StaticVer = basicInfo.StaticVer,
                StkDeliveryDays = basicInfo.StkDeliveryDays,
                SuspendedFlag = basicInfo.SuspendedFlag,
                StkId = new Quotation.SymbolCode(basicInfo.Code),
                TotalCirculatingShare = basicInfo.TotalCirculatingShare,
                TotalIssueQty = basicInfo.TotalIssueQty,
                TradeStageFlag = basicInfo.TradeStageFlag,
                TradeStatus = basicInfo.TradeStatus,
                TradeType = basicInfo.TradeType,
                TradeUnit = basicInfo.TradeUnit,
                StkType = basicInfo.Type,
                UpdateTime = basicInfo.UpdateTime,
                VoteFlag = basicInfo.VoteFlag,
                QuotLevel = basicInfo.QuotLevel

            };
        }

        public StockBasicInfo Convert(Quotation.Stock.BasicInfo basicInfo)
        {
            return new StockBasicInfo()
            {
                AccountDate = basicInfo.AccountDate.ToYMMDD(),
                AccuredInterest = basicInfo.AccuredInterest,
                AnnualProfit = basicInfo.AnnualProfit,
                BaseStock = basicInfo.BaseStock,
                BeginDate = basicInfo.BeginDate.ToYMMDD(),
                BondBonusDate = basicInfo.BondBonusDate.ToYMMDD(),
                BondConvertRate = basicInfo.BondConvertRate,
                ComponentStockFlag = basicInfo.ComponentStockFlag,
                ConvertQty = basicInfo.ConvertQty,
                CurrencyType = basicInfo.CurrencyType,
                Decimal = basicInfo.Decimal,
                DynamicVer = basicInfo.DynamicVer,
                EndDate = basicInfo.EndDate.ToYMMDD(),
                EnglishName = basicInfo.EnglishName,
                FinancingLoanFlag = basicInfo.FinancingLoanFlag,
                FinancingLoanTradingFlag = basicInfo.FinancingLoanTradingFlag,
                GuarantyConvertRate = basicInfo.GuarantyConvertRate,
                HighBuyQtyLimit = basicInfo.HighBuyQtyLimit,
                HighSellQtyLimit = basicInfo.HighSellQtyLimit,
                IndustryType = basicInfo.IndustryType,
                ISINCode = basicInfo.ISINCode,
                LastAnnualProfit = basicInfo.LastAnnualProfit,
                LevelFlag = basicInfo.LevelFlag,
                LowBuyQtyLimit = basicInfo.LowBuyQtyLimit,
                LowSellQtyLimit = basicInfo.LowSellQtyLimit,
                MarketMakerFlag = basicInfo.MarketMakerFlag,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinOrderPrice = basicInfo.MinOrderPrice,
                Name = basicInfo.Name,
                OpenPrice = basicInfo.OpenPrice,
                OrderPriceUnit = basicInfo.OrderPriceUnit,
                OtherBizStatus = basicInfo.OtherBizStatus,
                ParValue = basicInfo.ParValue,
                PreClosePrice = basicInfo.PreClosePrice,
                PreExchTotalKnockAmt = basicInfo.PreExchTotalKnockAmt,
                PreExchTotalKnockQty = basicInfo.PreExchTotalKnockQty,
                QuotationTime = basicInfo.QuotationTime,
                RepurchaseDay = basicInfo.RepurchaseDay,
                SecuritiesLoanFlag = basicInfo.SecuritiesLoanFlag,
                SecuritiesLoanSellPriceLimitFlag = basicInfo.SecuritiesLoanSellPriceLimitFlag,
                SecuritiesLoanTradingFlag = basicInfo.SecuritiesLoanTradingFlag,
                ShortPrefix = basicInfo.ShortPrefix,
                StaticVer = basicInfo.StaticVer,
                StkDeliveryDays = basicInfo.StkDeliveryDays,
                SuspendedFlag = basicInfo.SuspendedFlag,
                Code = basicInfo.StkId.LongCode,
                ExchId = basicInfo.StkId.ExchId,
                TotalCirculatingShare = basicInfo.TotalCirculatingShare,
                TotalIssueQty = basicInfo.TotalIssueQty,
                TradeStageFlag = basicInfo.TradeStageFlag,
                TradeStatus = basicInfo.TradeStatus,
                TradeType = basicInfo.TradeType,
                TradeUnit = basicInfo.TradeUnit,
                Type = basicInfo.StkType,
                UpdateTime = basicInfo.UpdateTime,
                VoteFlag = basicInfo.VoteFlag,
                QuotLevel = basicInfo.QuotLevel
            };
        }

        public Quotation.Stock.QuotationInfo Convert(StockQuotationInfo quotInfo)
        {
            var rtn = new Quotation.Stock.QuotationInfo()
            {
                QuotLevel = quotInfo.QuotLevel,
                StkId = new Quotation.SymbolCode(quotInfo.Code),
                Name = quotInfo.Name,
                BuyAvgPrice = quotInfo.BuyAvgPrice,
                SellAvgPrice = quotInfo.SellAvgPrice,
                InnerVolume = quotInfo.InnerVolume,
                OuterVolume = quotInfo.OuterVolume,
                NotDefineVolume = quotInfo.NotDefineVolume,
                SN = quotInfo.SN,
                QuotationTime = quotInfo.QuotationTime,
                UpdateTime = quotInfo.UpdateTime,
                Tick = Convert(quotInfo.Tick),
                BidAsk = new List<Quotation.BidAsk>()
            };
            if (quotInfo.BidAsk != null)
            {
                foreach (var bidAsk in quotInfo.BidAsk)
                {
                    rtn.BidAsk.Add(new Quotation.BidAsk()
                    {
                        Buy = new Quotation.QtyPrice() { Price = bidAsk.BuyPrice, Qty = bidAsk.BuyQty },
                        Sell = new Quotation.QtyPrice() { Price = bidAsk.SellPrice, Qty = bidAsk.SellQty }
                    });
                }
            }
            return rtn;
        }

        public StockQuotationInfo Convert(Quotation.Stock.QuotationInfo quotInfo)
        {
            var rtn = new StockQuotationInfo()
            {
                QuotLevel = quotInfo.QuotLevel,
                Code = quotInfo.StkId.LongCode,
                Name = quotInfo.Name,
                BuyAvgPrice = quotInfo.BuyAvgPrice,
                SellAvgPrice = quotInfo.SellAvgPrice,
                InnerVolume = quotInfo.InnerVolume,
                OuterVolume = quotInfo.OuterVolume,
                NotDefineVolume = quotInfo.NotDefineVolume,
                SN = quotInfo.SN,
                QuotationTime = quotInfo.QuotationTime,
                UpdateTime = quotInfo.UpdateTime,
                Tick = Convert(quotInfo.Tick),
                BidAsk = new List<OrderBook>()

            };
            if (quotInfo.BidAsk != null)
            {

                foreach (var bidAsk in quotInfo.BidAsk)
                {
                    rtn.BidAsk.Add(new OrderBook()
                    {
                        BuyPrice = bidAsk.Buy.Price,
                        BuyQty = bidAsk.Buy.Qty,
                        SellPrice = bidAsk.Sell.Price,
                        SellQty = bidAsk.Sell.Qty
                    }
                    );
                }

            }

            return rtn;
        }

        public Quotation.Stock.TickInfo Convert(StockTickInfo tickInfo)
        {
            return new Quotation.Stock.TickInfo()
            {
                TickSN = tickInfo.TickSN,
                KnockTime = Quotation.Time.FromHMMss(tickInfo.KnockTime),
                KnockPrice = tickInfo.KnockPrice,
                KnockAmt = tickInfo.KnockAmt,
                KnockQty = tickInfo.KnockQty,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                HighPrice = tickInfo.HighPrice,
                LowPrice = tickInfo.LowPrice,
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                RepoTotalKnockAmt = tickInfo.RepoTotalKnockAmt,
                IOPV = tickInfo.IOPV,
                TotalBuyQty = tickInfo.TotalBuyQty,
                TotalBuyQtyDiff = tickInfo.TotalBuyQtyDiff,
                TotalSellQty = tickInfo.TotalSellQty,
                TotalSellQtyDiff = tickInfo.TotalSellQtyDiff,
                TradesCount = tickInfo.TradesCount,
                VolumeDiff = tickInfo.VolumeDiff,
                VolumeRate = tickInfo.VolumeRate,
                BSType = tickInfo.BSType
            };
        }

        public StockTickInfo Convert(Quotation.Stock.TickInfo tickInfo)
        {
            return new StockTickInfo()
            {
                TickSN = tickInfo.TickSN,
                KnockTime = tickInfo.KnockTime.ToHMMss(),
                KnockPrice = tickInfo.KnockPrice,
                KnockAmt = tickInfo.KnockAmt,
                KnockQty = tickInfo.KnockQty,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                HighPrice = tickInfo.HighPrice,
                LowPrice = tickInfo.LowPrice,
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                RepoTotalKnockAmt = tickInfo.RepoTotalKnockAmt,
                IOPV = tickInfo.IOPV,
                TotalBuyQty = tickInfo.TotalBuyQty,
                TotalBuyQtyDiff = tickInfo.TotalBuyQtyDiff,
                TotalSellQty = tickInfo.TotalSellQty,
                TotalSellQtyDiff = tickInfo.TotalSellQtyDiff,
                TradesCount = tickInfo.TradesCount,
                VolumeDiff = tickInfo.VolumeDiff,
                VolumeRate = tickInfo.VolumeRate,
                BSType = tickInfo.BSType
            };
        }

        public Quotation.Stock.DailyData Convert(StockDailyData dailyData)
        {
            return new Quotation.Stock.DailyData()
            {
                StkId = new Quotation.SymbolCode(dailyData.Code),
                TradeDay = Quotation.Date.FromYMMDD(dailyData.TradeDay),
                ClosePrice = dailyData.ClosePrice,
                OpenPrice = dailyData.OpenPrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                TotalKnockQty = dailyData.TotalKnockQty,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                IOPV = dailyData.IOPV,
                PreClosePrice = dailyData.PreClosePrice
            };
        }

        public StockDailyData Convert(Quotation.Stock.DailyData dailyData)
        {
            return new StockDailyData()
            {
                Code = dailyData.StkId.LongCode,
                TradeDay = dailyData.TradeDay.ToYMMDD(),
                ClosePrice = dailyData.ClosePrice,
                OpenPrice = dailyData.OpenPrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                TotalKnockQty = dailyData.TotalKnockQty,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                IOPV = dailyData.IOPV,
                PreClosePrice = dailyData.PreClosePrice
            };
        }



        public Quotation.Stock.MinuteData Convert(StockMinuteData minuteData, Quotation.SymbolCode stkId)
        {
            return new Quotation.Stock.MinuteData()
            {
                StkId = stkId,
                OccurTime = Quotation.Time.FromHMM(minuteData.OccurTime),
                TickSN = minuteData.TickSN,
                ClosePrice = minuteData.ClosePrice,
                OpenPrice = minuteData.OpenPrice,
                HighPrice = minuteData.HighPrice,
                LowPrice = minuteData.LowPrice,
                KnockAvgPrice = minuteData.KnockAvgPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockQty = minuteData.KnockQty,
                IOPV = minuteData.IOPV
            };
        }

        public StockMinuteData Convert(Quotation.Stock.MinuteData minuteData)
        {
            return new StockMinuteData()
            {
                OccurTime = (short)minuteData.OccurTime.ToHMM(),
                TickSN = minuteData.TickSN,
                ClosePrice = minuteData.ClosePrice,
                OpenPrice = minuteData.OpenPrice,
                HighPrice = minuteData.HighPrice,
                LowPrice = minuteData.LowPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockQty = minuteData.KnockQty,
                IOPV = minuteData.IOPV,
                KnockAvgPrice = minuteData.KnockAvgPrice
            };
        }


        public Quotation.Stock.MinuteDataCollection Convert(StockMinuteDataCollection minuteDataCollection)
        {
            Quotation.Stock.MinuteDataCollection rtn = new Quotation.Stock.MinuteDataCollection()
            {
                StkId = new Quotation.SymbolCode(minuteDataCollection.Code),
                TradeDay = Quotation.Date.FromYMMDD(minuteDataCollection.TradeDay),
                MinuteData = new List<Quotation.Stock.MinuteData>()
            };
            if (minuteDataCollection.MinuteData != null)
            {
                foreach (var md in minuteDataCollection.MinuteData)
                {
                    rtn.MinuteData.Add(Convert(md,rtn.StkId));
                }
            }

            return rtn;
        }

        public StockMinuteDataCollection Convert(Quotation.Stock.MinuteDataCollection minuteDataCollection)
        {
            StockMinuteDataCollection rtn = new StockMinuteDataCollection()
            {
                Code = minuteDataCollection.StkId.LongCode,
                TradeDay = minuteDataCollection.TradeDay.ToYMMDD(),
                MinuteData = new List<StockMinuteData> ()
            };
            if (minuteDataCollection.MinuteData != null)
            {
                foreach (var md in minuteDataCollection.MinuteData)
                {
                    rtn.MinuteData.Add(Convert(md));
                }
            }

            return rtn;
        }
    }
}
